const cache = () => {
}

cache.keyPrefix = 'joncc_'

cache.get = (key) => {
    const v = window.localStorage.getItem(cache.getKey(key))
    if (null === v) {
        return null
    }

    const {value, expire} = JSON.parse(v)
    if (0 === expire) {
        return value
    } else if (cache.time() <= expire) {
        return value
    } else {
        cache.remove(key)
        return null
    }
}

cache.set = (key, value, expire = 0) => {
    if (0 !== expire) {
        expire = cache.time() + expire
    }
    let data = {value, expire}
    window.localStorage.setItem(cache.getKey(key), JSON.stringify(data))
}

cache.getKey = (key) => {
    return cache.keyPrefix + key;
}

cache.remove = (key) => {
    window.localStorage.removeItem(cache.getKey(key))
}

cache.time = () => {
    return Math.round(new Date().getTime() / 1000)
}

cache.clear = () => {
    window.localStorage.clear()
}


export default cache